package com.tsingtree.modular.controller.view;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.tsingtree.base.auth.annotion.Permission;
import com.tsingtree.base.pojo.page.LayuiPageInfo;
import com.tsingtree.modular.entity.NanData;
import com.tsingtree.modular.entity.NanUser;
import com.tsingtree.modular.model.params.NanDataParam;
import com.tsingtree.modular.service.NanDataService;
import com.tsingtree.modular.service.NanUserDeviceService;
import com.tsingtree.modular.service.NanUserService;
import com.tsingtree.roses.core.base.controller.BaseController;
import com.tsingtree.roses.core.reqres.response.ResponseData;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.time.temporal.TemporalAdjusters;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


/**
 * 数据统计控制器
 *
 * @author jagger
 */
@Controller
@RequestMapping("/nanData")
public class NanDataController extends BaseController {

    private String PREFIX = "/nanData";

    @Autowired
    private NanDataService nanDataService;
    @Autowired
    private NanUserService nanUserService;
    @Autowired
    private NanUserDeviceService nanUserDeviceService;

    /**
     * 跳转到主页面
     *
     * @author jagger
     */
    @RequestMapping("")
    public String index() {
        return PREFIX + "/nanData.html";
    }

    @RequestMapping("/chart")
    public String chart() {
        return PREFIX + "/nanData_chart.html";
    }

    /**
     * 新增页面
     *
     * @author jagger
     */
    @RequestMapping("/add")
    public String add() {
        return PREFIX + "/nanData_add.html";
    }

    /**
     * 编辑页面
     *
     * @author jagger
     */
    @RequestMapping("/edit")
    public String edit() {
        return PREFIX + "/nanData_edit.html";
    }

    /**
     * 新增接口
     *
     * @author jagger
     */
    @RequestMapping("/addItem")
    @Permission
    @ResponseBody
    public ResponseData addItem(NanDataParam nanDataParam) {
        this.nanDataService.add(nanDataParam);
        return ResponseData.success();
    }

    /**
     * 编辑接口
     *
     * @author jagger
     */
    @RequestMapping("/editItem")
    @Permission
    @ResponseBody
    public ResponseData editItem(NanDataParam nanDataParam) {
        this.nanDataService.update(nanDataParam);
        return ResponseData.success();
    }

    /**
     * 删除接口
     *
     * @author jagger
     */
    //@RequestMapping("/delete")
    //@Permission
    //@ResponseBody
    public ResponseData delete(NanDataParam nanDataParam) {
        this.nanDataService.delete(nanDataParam);
        return ResponseData.success();
    }

    /**
     * 查看详情接口
     *
     * @author jagger
     */
    @RequestMapping("/detail")
    @ResponseBody
    public ResponseData detail(NanDataParam nanDataParam) {
        NanData detail = this.nanDataService.getById(nanDataParam.getId());
        return ResponseData.success(detail);
    }

    /**
     * 查询列表
     *
     * @author jagger
     */
    @ResponseBody
    @Permission
    @RequestMapping("/list")
    public LayuiPageInfo list(NanDataParam nanDataParam) {
        return this.nanDataService.findPageBySpec(nanDataParam);
    }


    /**
     * 获取条形图数据
     */
    @ResponseBody
    @Permission(link = "/list")
    @RequestMapping("/chartData")
    public ResponseData chartData(String date) {
        String compareStr;
        String format;
        if("day".equals(date)){
            compareStr = LocalDateTime.now().minusDays(30).format(DateTimeFormatter.ofPattern("yyyy-MM-dd"));
            format = "%Y-%m-%d";
        }else{
            compareStr = LocalDateTime.now().minusYears(1).format(DateTimeFormatter.ofPattern("yyyy-MM-dd"));
            format = "%Y-%m";
        }
        QueryWrapper<NanData> qw = new QueryWrapper<NanData>().select("sum(reg_num) as value, sum(bind_num) bindNum, " +
            "sum(banner_one_show_num) bannerOneShowNum," +
            "sum(banner_two_show_num) bannerTwoShowNum," +
            "sum(banner_three_show_num) bannerThreeShowNum," +
            "sum(banner_one_click_num) bannerOneClickNum," +
            "sum(banner_two_click_num) bannerTwoClickNum," +
            "sum(banner_three_click_num) bannerThreeClickNum," +
            "sum(stop_community_time) stopCommunityTime," +
            "sum(stop_game_time) stopGameTime," +
            "sum(stop_interact_time) stopInteractTime," +
            "sum(stop_chat_time) stopChatTime," +
            "sum(stop_community_num) stopCommunityNum," +
            "sum(stop_game_num) stopGameNum," +
            "sum(stop_interact_num) stopInteractNum," +
            "sum(stop_chat_num) stopChatNum," +
            " DATE_FORMAT(create_time, '" + format + "') as name")
            .gt("create_time", compareStr)
            .groupBy("DATE_FORMAT(create_time, '" + format + "')");
        List<Map<String, Object>> maps = nanDataService.listMaps(qw);
        return ResponseData.success(maps);
    }

    /**
     * 返回总览数据
     */
    @ResponseBody
    @Permission(link = "/list")
    @RequestMapping("/data")
    public ResponseData data() {
        Map<String, Object> map = new HashMap<>();
        long userTotal = nanUserService.count();
        long userDataBeforeMonth = 0;
        long userDataCurrentMonth = 0;
        long userDataYesterday = 0;
        long userDataToday = 0;
        List<Map<String, Object>> sexList = nanUserService.listMaps(new QueryWrapper<NanUser>().select("sex as name", "count(1) as value").groupBy("sex"));;
        List<Map<String, Object>> countryList = nanUserService.listMaps(new QueryWrapper<NanUser>().select("country as name", "count(1) as value").groupBy("country"));
        long deviceTotal = nanUserDeviceService.count();
        long deviceDataBeforeMonth = 0;
        long deviceDataCurrentMonth = 0;
        long deviceDataYesterday = 0;
        long deviceDataToday = 0;

        long bannerTodayShowOne = 0;
        long bannerTodayShowTwo = 0;
        long bannerTodayShowThree = 0;
        long bannerTodayClickOne = 0;
        long bannerTodayClickTwo = 0;
        long bannerTodayClickThree = 0;
        long bannerYesterdayShowOne = 0;
        long bannerYesterdayShowTwo = 0;
        long bannerYesterdayShowThree = 0;
        long bannerYesterdayClickOne = 0;
        long bannerYesterdayClickTwo = 0;
        long bannerYesterdayClickThree = 0;
        long bannerBeforeMonthShowOne = 0;
        long bannerBeforeMonthShowTwo = 0;
        long bannerBeforeMonthShowThree = 0;
        long bannerBeforeMonthClickOne = 0;
        long bannerBeforeMonthClickTwo = 0;
        long bannerBeforeMonthClickThree = 0;
        long bannerCurrentMonthShowOne = 0;
        long bannerCurrentMonthShowTwo = 0;
        long bannerCurrentMonthShowThree = 0;
        long bannerCurrentMonthClickOne = 0;
        long bannerCurrentMonthClickTwo = 0;
        long bannerCurrentMonthClickThree = 0;
        long stopCommunityTimeToday = 0;
        long stopCommunityTimeYesterday = 0;
        long stopCommunityTimeBeforeMonth = 0;
        long stopCommunityTimeCurrentMonth = 0;
        long stopGameTimeToday = 0;
        long stopGameTimeYesterday = 0;
        long stopGameTimeBeforeMonth = 0;
        long stopGameTimeCurrentMonth = 0;
        long stopInteractTimeToday = 0;
        long stopInteractTimeYesterday = 0;
        long stopInteractTimeBeforeMonth = 0;
        long stopInteractTimeCurrentMonth = 0;
        long stopChatTimeToday = 0;
        long stopChatTimeYesterday = 0;
        long stopChatTimeBeforeMonth = 0;
        long stopChatTimeCurrentMonth = 0;
        long stopCommunityNumToday = 0;
        long stopCommunityNumYesterday = 0;
        long stopCommunityNumBeforeMonth = 0;
        long stopCommunityNumCurrentMonth = 0;
        long stopGameNumToday = 0;
        long stopGameNumYesterday = 0;
        long stopGameNumBeforeMonth = 0;
        long stopGameNumCurrentMonth = 0;
        long stopInteractNumToday = 0;
        long stopInteractNumYesterday = 0;
        long stopInteractNumBeforeMonth = 0;
        long stopInteractNumCurrentMonth = 0;
        long stopChatNumToday = 0;
        long stopChatNumYesterday = 0;
        long stopChatNumBeforeMonth = 0;
        long stopChatNumCurrentMonth = 0;
        LocalDate today = LocalDate.now();
        LocalDate firstDayOfThisMonth = today.with(TemporalAdjusters.firstDayOfMonth());
        LocalDateTime firstDayDateTime = firstDayOfThisMonth.atStartOfDay();
        LocalDate firstDayOfLastMonth = firstDayOfThisMonth.minusMonths(1);
        List<NanData> list = this.nanDataService.list(new QueryWrapper<NanData>().ge("create_time", firstDayOfLastMonth).orderByDesc("create_time"));
        for (int i = 0; i < list.size(); i++) {
            NanData nanData = list.get(i);
            if(i == 0){
                userDataToday = nanData.getRegNum();
                deviceDataToday = nanData.getBindNum();
                bannerTodayShowOne = nanData.getBannerOneShowNum();
                bannerTodayShowTwo = nanData.getBannerTwoShowNum();
                bannerTodayShowThree = nanData.getBannerThreeShowNum();
                bannerTodayClickOne = nanData.getBannerOneClickNum();
                bannerTodayClickTwo = nanData.getBannerTwoClickNum();
                bannerTodayClickThree = nanData.getBannerThreeClickNum();
                stopCommunityTimeToday = nanData.getStopCommunityTime();
                stopGameTimeToday = nanData.getStopGameTime();
                stopInteractTimeToday = nanData.getStopInteractTime();
                stopChatTimeToday = nanData.getStopChatTime();
                stopCommunityNumToday = nanData.getStopCommunityNum();
                stopGameNumToday = nanData.getStopGameNum();
                stopInteractNumToday = nanData.getStopInteractNum();
                stopChatNumToday = nanData.getStopChatNum();
            }
            if(i == 1){
                userDataYesterday = nanData.getRegNum();
                deviceDataYesterday = nanData.getBindNum();
                bannerYesterdayShowOne = nanData.getBannerOneShowNum();
                bannerYesterdayShowTwo = nanData.getBannerTwoShowNum();
                bannerYesterdayShowThree = nanData.getBannerThreeShowNum();
                bannerYesterdayClickOne = nanData.getBannerOneClickNum();
                bannerYesterdayClickTwo = nanData.getBannerTwoClickNum();
                bannerYesterdayClickThree = nanData.getBannerThreeClickNum();
                stopCommunityTimeYesterday = nanData.getStopCommunityTime();
                stopGameTimeYesterday = nanData.getStopGameTime();
                stopInteractTimeYesterday = nanData.getStopInteractTime();
                stopChatTimeYesterday = nanData.getStopChatTime();
                stopCommunityNumYesterday = nanData.getStopCommunityNum();
                stopGameNumYesterday = nanData.getStopGameNum();
                stopInteractNumYesterday = nanData.getStopInteractNum();
                stopChatNumYesterday = nanData.getStopChatNum();
            }
            if (nanData.getCreateTime().isBefore(firstDayDateTime)) {
                userDataBeforeMonth += nanData.getRegNum();
                deviceDataBeforeMonth += nanData.getBindNum();
                bannerBeforeMonthShowOne += nanData.getBannerOneShowNum();
                bannerBeforeMonthShowTwo += nanData.getBannerTwoShowNum();
                bannerBeforeMonthShowThree += nanData.getBannerThreeShowNum();
                bannerBeforeMonthClickOne += nanData.getBannerOneClickNum();
                bannerBeforeMonthClickTwo += nanData.getBannerTwoClickNum();
                bannerBeforeMonthClickThree += nanData.getBannerThreeClickNum();
                stopCommunityTimeBeforeMonth += nanData.getStopCommunityTime();
                stopGameTimeBeforeMonth += nanData.getStopGameTime();
                stopInteractTimeBeforeMonth += nanData.getStopInteractTime();
                stopChatTimeBeforeMonth += nanData.getStopChatTime();
                stopCommunityNumBeforeMonth += nanData.getStopCommunityNum();
                stopGameNumBeforeMonth += nanData.getStopGameNum();
                stopInteractNumBeforeMonth += nanData.getStopInteractNum();
                stopChatNumBeforeMonth += nanData.getStopChatNum();
            }else{
                userDataCurrentMonth += nanData.getRegNum();
                deviceDataCurrentMonth += nanData.getBindNum();
                bannerCurrentMonthShowOne += nanData.getBannerOneShowNum();
                bannerCurrentMonthShowTwo += nanData.getBannerTwoShowNum();
                bannerCurrentMonthShowThree += nanData.getBannerThreeShowNum();
                bannerCurrentMonthClickOne += nanData.getBannerOneClickNum();
                bannerCurrentMonthClickTwo += nanData.getBannerTwoClickNum();
                bannerCurrentMonthClickThree += nanData.getBannerThreeClickNum();
                stopCommunityTimeCurrentMonth += nanData.getStopCommunityTime();
                stopGameTimeCurrentMonth += nanData.getStopGameTime();
                stopInteractTimeCurrentMonth += nanData.getStopInteractTime();
                stopChatTimeCurrentMonth += nanData.getStopChatTime();
                stopCommunityNumCurrentMonth += nanData.getStopCommunityNum();
                stopGameNumCurrentMonth += nanData.getStopGameNum();
                stopInteractNumCurrentMonth += nanData.getStopInteractNum();
                stopChatNumCurrentMonth += nanData.getStopChatNum();

            }
        }
        long bannerTodayShow = bannerTodayShowOne + bannerTodayShowTwo + bannerTodayShowThree;
        long bannerTodayClick = bannerTodayClickOne + bannerTodayClickTwo + bannerTodayClickThree;
        long bannerYesterdayShow = bannerYesterdayShowOne + bannerYesterdayShowTwo + bannerYesterdayShowThree;
        long bannerYesterdayClick = bannerYesterdayClickOne + bannerYesterdayClickTwo + bannerYesterdayClickThree;
        long bannerBeforeMonthShow = bannerBeforeMonthShowOne + bannerBeforeMonthShowTwo + bannerBeforeMonthShowThree;
        long bannerBeforeMonthClick = bannerBeforeMonthClickOne + bannerBeforeMonthClickTwo + bannerBeforeMonthClickThree;
        long bannerCurrentMonthShow = bannerCurrentMonthShowOne + bannerCurrentMonthShowTwo + bannerCurrentMonthShowThree;
        long bannerCurrentMonthClick = bannerCurrentMonthClickOne + bannerCurrentMonthClickTwo + bannerCurrentMonthClickThree;
        map.put("userTotal", userTotal);
        map.put("userDataBeforeMonth", userDataBeforeMonth);
        map.put("userDataCurrentMonth", userDataCurrentMonth);
        map.put("userDataYesterday", userDataYesterday);
        map.put("userDataToday", userDataToday);
        map.put("deviceTotal", deviceTotal);
        map.put("deviceDataBeforeMonth", deviceDataBeforeMonth);
        map.put("deviceDataCurrentMonth", deviceDataCurrentMonth);
        map.put("deviceDataYesterday", deviceDataYesterday);
        map.put("deviceDataToday", deviceDataToday);
        map.put("bannerTodayShowOne", bannerTodayShowOne);
        map.put("bannerTodayShowTwo", bannerTodayShowTwo);
        map.put("bannerTodayShowThree", bannerTodayShowThree);
        map.put("bannerTodayClickOne", bannerTodayClickOne);
        map.put("bannerTodayClickTwo", bannerTodayClickTwo);
        map.put("bannerTodayClickThree", bannerTodayClickThree);
        map.put("bannerYesterdayShowOne", bannerYesterdayShowOne);
        map.put("bannerYesterdayShowTwo", bannerYesterdayShowTwo);
        map.put("bannerYesterdayShowThree", bannerYesterdayShowThree);
        map.put("bannerYesterdayClickOne", bannerYesterdayClickOne);
        map.put("bannerYesterdayClickTwo", bannerYesterdayClickTwo);
        map.put("bannerYesterdayClickThree", bannerYesterdayClickThree);
        map.put("bannerBeforeMonthShowOne", bannerBeforeMonthShowOne);
        map.put("bannerBeforeMonthShowTwo", bannerBeforeMonthShowTwo);
        map.put("bannerBeforeMonthShowThree", bannerBeforeMonthShowThree);
        map.put("bannerBeforeMonthClickOne", bannerBeforeMonthClickOne);
        map.put("bannerBeforeMonthClickTwo", bannerBeforeMonthClickTwo);
        map.put("bannerBeforeMonthClickThree", bannerBeforeMonthClickThree);
        map.put("bannerCurrentMonthShowOne", bannerCurrentMonthShowOne);
        map.put("bannerCurrentMonthShowTwo", bannerCurrentMonthShowTwo);
        map.put("bannerCurrentMonthShowThree", bannerCurrentMonthShowThree);
        map.put("bannerCurrentMonthClickOne", bannerCurrentMonthClickOne);
        map.put("bannerCurrentMonthClickTwo", bannerCurrentMonthClickTwo);
        map.put("bannerCurrentMonthClickThree", bannerCurrentMonthClickThree);
        map.put("bannerTodayShow", bannerTodayShow);
        map.put("bannerTodayClick", bannerTodayClick);
        map.put("bannerYesterdayShow", bannerYesterdayShow);
        map.put("bannerYesterdayClick", bannerYesterdayClick);
        map.put("bannerBeforeMonthShow", bannerBeforeMonthShow);
        map.put("bannerBeforeMonthClick", bannerBeforeMonthClick);
        map.put("bannerCurrentMonthShow", bannerCurrentMonthShow);
        map.put("bannerCurrentMonthClick", bannerCurrentMonthClick);
        map.put("sexList", sexList);
        map.put("countryList", countryList);
        map.put("stopCommunityTimeToday", stopCommunityTimeToday);
        map.put("stopCommunityTimeYesterday", stopCommunityTimeYesterday);
        map.put("stopCommunityTimeBeforeMonth", stopCommunityTimeBeforeMonth);
        map.put("stopCommunityTimeCurrentMonth", stopCommunityTimeCurrentMonth);
        map.put("stopCommunityNumToday", stopCommunityNumToday);
        map.put("stopCommunityNumYesterday", stopCommunityNumYesterday);
        map.put("stopCommunityNumBeforeMonth", stopCommunityNumBeforeMonth);
        map.put("stopCommunityNumCurrentMonth", stopCommunityNumCurrentMonth);
        map.put("stopGameTimeToday", stopGameTimeToday);
        map.put("stopGameTimeYesterday", stopGameTimeYesterday);
        map.put("stopGameTimeBeforeMonth", stopGameTimeBeforeMonth);
        map.put("stopGameTimeCurrentMonth", stopGameTimeCurrentMonth);
        map.put("stopGameNumToday", stopGameNumToday);
        map.put("stopGameNumYesterday", stopGameNumYesterday);
        map.put("stopGameNumBeforeMonth", stopGameNumBeforeMonth);
        map.put("stopGameNumCurrentMonth", stopGameNumCurrentMonth);
        map.put("stopInteractTimeToday", stopInteractTimeToday);
        map.put("stopInteractTimeYesterday", stopInteractTimeYesterday);
        map.put("stopInteractTimeBeforeMonth", stopInteractTimeBeforeMonth);
        map.put("stopInteractTimeCurrentMonth", stopInteractTimeCurrentMonth);
        map.put("stopInteractNumToday", stopInteractNumToday);
        map.put("stopInteractNumYesterday", stopInteractNumYesterday);
        map.put("stopInteractNumBeforeMonth", stopInteractNumBeforeMonth);
        map.put("stopInteractNumCurrentMonth", stopInteractNumCurrentMonth);
        map.put("stopChatTimeToday", stopChatTimeToday);
        map.put("stopChatTimeYesterday", stopChatTimeYesterday);
        map.put("stopChatTimeBeforeMonth", stopChatTimeBeforeMonth);
        map.put("stopChatTimeCurrentMonth", stopChatTimeCurrentMonth);
        map.put("stopChatNumToday", stopChatNumToday);
        map.put("stopChatNumYesterday", stopChatNumYesterday);
        map.put("stopChatNumBeforeMonth", stopChatNumBeforeMonth);
        map.put("stopChatNumCurrentMonth", stopChatNumCurrentMonth);
        return ResponseData.success(map);
    }


}


